/*
 * LiquidBounce Hacked Client
 * A free open source mixin-based injection hacked client for Minecraft using Minecraft Forge.
 * https://github.com/CCBlueX/LiquidBounce/
 */
package net.ccbluex.liquidbounce.features.module.modules.exploit

import net.ccbluex.liquidbounce.api.minecraft.client.gui.inventory.IGuiContainer
import net.ccbluex.liquidbounce.event.EventTarget
import net.ccbluex.liquidbounce.event.KeyEvent
import net.ccbluex.liquidbounce.event.PacketEvent
import net.ccbluex.liquidbounce.event.ScreenEvent
import net.ccbluex.liquidbounce.features.module.Module
import net.ccbluex.liquidbounce.features.module.ModuleCategory
import net.ccbluex.liquidbounce.features.module.ModuleInfo
import org.lwjgl.input.Keyboard

@ModuleInfo(name = "KeepContainer", description = "Allows you to open a formerly closed inventory container everywhere. (Press INSERT Key to open)", category = ModuleCategory.EXPLOIT)
class KeepContainer : Module() {
    private var container: IGuiContainer? = null

    override fun onDisable() {
        if (container != null)
            mc.netHandler.addToSendQueue(classProvider.createCPacketCloseWindow(container!!.inventorySlots!!.windowId))

        container = null
    }

    @EventTarget
    fun onGui(event: ScreenEvent) {
        val guiScreen = event.guiScreen

        if (classProvider.isGuiContainer(guiScreen) && !classProvider.isGuiInventory(guiScreen))
            container = guiScreen!!.asGuiContainer()
    }

    @EventTarget
    fun onKey(event: KeyEvent) {
        if (event.key == Keyboard.KEY_INSERT) {
            if (container == null)
                return

            mc.displayGuiScreen(container)
        }
    }

    @EventTarget
    fun onPacket(event: PacketEvent) {
        if (classProvider.isCPacketCloseWindow(event.packet))
            event.cancelEvent()
        else if (classProvider.isSPacketCloseWindow(event.packet)) {
            val packetCloseWindow = event.packet.asSPacketCloseWindow()

            if (container != null && container!!.inventorySlots != null && packetCloseWindow.windowId == container!!.inventorySlots!!.windowId)
                container = null
        }
    }
}